package com.sx.zongzhi;

import android.app.Activity;
import android.app.Application;
import android.os.Environment;

import com.zero.smallvideorecord.DeviceUtils;
import com.zero.smallvideorecord.JianXiCamera;

import java.io.File;
import java.util.Stack;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by XZY on 2017/11/21.
 */

public class MyApplication extends Application {

    public static MyApplication context;
    public static Stack<Activity> activityStack = new Stack<>();
    public static MyApplication instance;
    public static  String sid="";

//    private RefWatcher refWatcher;
//    public static RefWatcher getRefWatcher(Context context) {
//        return instance.refWatcher;
//    }
    @Override
    public void onCreate() {
        super.onCreate();
        context=this;
        instance=this;
        JPushInterface.init(this);
        JPushInterface.setDebugMode(false);
        initSmallVideo();
//        refWatcher = LeakCanary.install(this);
    }

    /**
     * 初始化视频拍摄
     */
    private void initSmallVideo() {
        // 设置拍摄视频缓存路径
        File dcim = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        if (DeviceUtils.isZte()) {
            if (dcim.exists()) {
                JianXiCamera.setVideoCachePath(dcim + "/zero/");
            } else {
                JianXiCamera.setVideoCachePath(dcim.getPath().replace("/sdcard/",
                        "/sdcard-ext/")
                        + "/zero/");
            }
        } else {
            JianXiCamera.setVideoCachePath(dcim + "/zero/");
        }
        // 初始化拍摄
        JianXiCamera.initialize(false, null);
    }

    /**
     * 添加Activity到堆栿
     */
    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new Stack<>();
        }
        if (!activityStack.contains(activity)) {
            activityStack.add(activity);
        }
    }

    /**
     * 添加Activity到堆栿
     */
    public void removeActivity(Activity activity) {
        if (activityStack.contains(activity)) {
            activityStack.remove(activity);
        }
    }



    public void finishAllActivity() {
        if (activityStack != null && activityStack.size() > 0) {
            for (Activity activity : activityStack) {
                activity.finish();
            }
        }
    }
}
